package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.common.constants.WmNewsMessageConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Description:
 * @Version: V1.0
 */
@Component
@Slf4j
public class ArticleIsDownListener {

    @Autowired
    ApArticleConfigService apArticleConfigService;

    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void receiveMessage(String message) {

        log.info("ArticleIsDownListener receiveMessage:{}", message);

        if (StringUtils.isNotBlank(message)) {
            Map<String, Object> map = JSON.parseObject(message, Map.class);

            // 处理上下架的状态
            Object enable = map.get("enable");

            boolean isDown = true;  // 下架
            if (enable.equals(1)) {  // 上架
                isDown = false;
            }

            // 根据文章的id修改文章的上下架状态
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    .eq(ApArticleConfig::getArticleId, map.get("articleId"))
                    .set(ApArticleConfig::getIsDown,isDown )
            );
        }

    }

}
